/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao.erp;

import com.openbravo.data.loader.query.QBFParameter;
import e.odbo.data.dao.ViewDAO;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sqlbuilder.QBFBuilder;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasic;
import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.bean.erp.viewbean.OrderLine_V;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class OrderLine_VDAO extends ViewDAO<OrderLine_V>{

    public OrderLine_VDAO(I_Session s) {
        super(s);
    }

    public OrderLine_V readValues(DataRead dr,OrderLine_V p) throws BasicException {
        if(p==null)
        	p=new OrderLine_V();
        p.orderid = dr.getString(1);
        p.defineType= dr.getString(2);
        p.ordertype = dr.getInt(3);
        p.date = dr.getTimestamp(4);
        p.location=dr.getString(5);
        p.name = dr.getString(6);
        p.customer = dr.getString(7);
        p.defaultPrice=dr.getDouble(8);
        p.consulMultiply=dr.getDouble(9);
        p.attributeset=dr.getString(10);
        p.multiply=dr.getDouble(11);
        p.price=dr.getDouble(12);
        return p;
    }

   public List<OrderLine_V> productTransactionDialyListQBF(Object filter)throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
                "SELECT T.ORDERID, E.NAME, T.ORDERTYPE, T.CURDATE, S.NAME, P.NAME, C.NAME, L.DEFAULTPRICE, L.CONSULUNIT, ATS.VALUE, SUM(L.UNITS), MAX(L.PRICE)  "
                + "FROM ORDERLINES L LEFT JOIN ORDERS T ON L.ORDERRID = T.ID LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN LOCATIONS S ON S.ID = T.LOCATIONREF LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID LEFT OUTER JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID "
                + "LEFT JOIN PRODUCTS PS ON L.PRODUCT = PS.ID "
                + "LEFT OUTER JOIN  ATTRIBUTEINSTANCE ATS ON L.ATTRIBUTESETINSTANCE_ID=ATS.ID "
                + "WHERE  ?(QBF_FILTER)  "
                + "GROUP BY T.ORDERID, E.NAME, T.ORDERTYPE, T.CURDATE, S.NAME, P.NAME, C.NAME, L.DEFAULTPRICE, L.CONSULUNIT,ATS.VALUE", new String[]{"T.CURDATE", "T.CURDATE", "T.CUSTOMER", "T.LOCATIONREF", "T.DEFINETYPEREF", "T.PERSON", "L.PRODUCT", "PS.CATEGORY"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.OBJECTS}), this).list(filter);
    }

    public  List<OrderLine_V> productPOSTransactionDialyListQBF(Object filter)throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
                "SELECT T.TICKETID, 'POS' AS POS, T.TICKETTYPE, T.CURDATE, S.NAME, P.NAME, C.NAME, L.DEFAULTPRICE, 0 AS CONSULUNIT, ATS.VALUE, SUM(L.UNITS), MAX(L.PRICE)  "
                + "FROM TICKETLINES L LEFT JOIN TICKETS T ON L.TICKET = T.ID LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN LOCATIONS S ON S.ID = T.LOCATIONREF LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID "
                + "LEFT JOIN PRODUCTS PS ON L.PRODUCT = PS.ID "
                + "LEFT OUTER JOIN  ATTRIBUTEINSTANCE ATS ON L.ATTRIBUTESETINSTANCE_ID=ATS.ID "
                + "WHERE  ?(QBF_FILTER)  "
                + "GROUP BY T.TICKETID, POS, T.TICKETTYPE, T.CURDATE, S.NAME, P.NAME, C.NAME, L.DEFAULTPRICE , CONSULUNIT,ATS.VALUE", new String[]{"T.CURDATE", "T.CURDATE", "T.CUSTOMER", "T.LOCATIONREF", "T.PERSON", "L.PRODUCT", "PS.CATEGORY"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.OBJECTS}), this).list(filter);
    }


    public List<OrderLine_V> list(Object filter) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
                "SELECT T.ORDERID, E.NAME, T.ORDERTYPE, T.CURDATE, S.NAME, P.NAME, C.NAME, L.DEFAULTPRICE, L.CONSULUNIT,ATS.VALUE, L.UNITS, L.PRICE  "
                + "FROM ORDERLINES L LEFT OUTER JOIN ORDERS T ON L.ORDERRID = T.ID LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN LOCATIONS S ON S.ID = T.LOCATIONREF LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID LEFT OUTER JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID "
                + "LEFT OUTER JOIN  ATTRIBUTEINSTANCE ATS ON L.ATTRIBUTESETINSTANCE_ID=ATS.ID "
                + "WHERE ?(QBF_FILTER)  ORDER BY T.CURDATE DESC ", new String[]{"T.ORDERID", "T.DEFINETYPEREF", "T.ORDERTYPE", "T.CURDATE", "T.CURDATE", "P.NAME", "C.NAME", "T.LOCATIONREF", "T.TOTALPRICE", "L.PRODUCT"}), new SerializerWriteBasic(Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.INT, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.DOUBLE, Datas.OBJECT, Datas.STRING), this).list(filter);
    }

    @Override
    public Class getSuportClass() {
       return OrderLine_V.class;
    }


    @Override
    public List<OrderLine_V> query(QBFParameter filter) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
                "SELECT T.ORDERID, E.NAME, T.ORDERTYPE, T.CURDATE, S.NAME, P.NAME, C.NAME, L.DEFAULTPRICE, L.CONSULUNIT,ATS.VALUE, L.UNITS, L.PRICE  "
                        + "FROM ORDERLINES L LEFT OUTER JOIN ORDERS T ON L.ORDERRID = T.ID LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN LOCATIONS S ON S.ID = T.LOCATIONREF LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID LEFT OUTER JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID "
                        + "LEFT OUTER JOIN  ATTRIBUTEINSTANCE ATS ON L.ATTRIBUTESETINSTANCE_ID=ATS.ID "
                        + "WHERE ?(QBF_FILTER)  ORDER BY T.CURDATE DESC "), null, this).list(filter);

    }
}
